//
//  main.cpp
//  DS_Demo
//
//  Created by apple on 2023/9/28.
//

#include <iostream>
#include "./src/List/SqList.hpp"

void test_sqlist(){
    int num=0;
    int data[1024]={-1};
    SqList L;
    InitList(&L);
    std::cout<<"输入数量num:";
    std::cin>>num;
    std::cout<<"输入"<<num<<"个int类型的数字:";
    for(int i = 0; i<num; ++i)
        std::cin>>data[i];
    std::cout<<"输出当前线性表中的数据:";
    for(int i = 0; i<num;++i)
    {
        ListInsert(&L, i, data[i]);
    }
    ListTraverse(L);
    //////////////////////////////////////////////////////////////////////
    // insert
    int pos = 0;
    std::cout<<"(0<=pos<="<<L.length<<")"<<"输入插入位置pos:";
    std::cin>>pos;
    std::cout<<"在线性表的第"<<pos<<"个位置上插入一个数字elem:";
    int elem = 0;
    std::cin>>elem;
    ListInsert(&L, pos-1, elem);
    ListTraverse(L);
    //////////////////////////////////////////////////////
    //delete
    std::cout<<"(0<=pos"<<L.length<<")"<<"输入删除位置pos:";
    std::cin>>pos;
    
    int ret = ListDelete(&L, pos-1, &elem);
    std::cout<<"在线性表的第"<<pos<<"个位置上删除一个数字elem:"<<elem<<std::endl;
    if(ret == OK)
        std::cout<<"删除成功"<<std::endl;
    else std::cout<<"删除失败"<<std::endl;
    ListTraverse(L);
    // LocateElem
    std::cout<<"输入一个元素:";
    std::cin>>elem;
    std::cout<<"查看"<<elem<<"是否在当前线性表中?"<<std::endl;
    if(LocateElem(L, elem) == -1){
        std::cout<<elem<<"不在当前列表中。"<<std::endl;
    }else{
        std::cout<<elem<<"在当前列表中的第"<<LocateElem(L, elem)<<"个位置。"<<std::endl;
    }
    
    // next elem And perior elem
    int next, prior;
    PriorElem(L, elem, &prior);
    NextElem(L, elem, &next);
    std::cout<<"当前元素"<<elem<<"的直接前驱是"<<prior<<",直接后继是"<<next<<std::endl;
    
    // Union La Lb
    SqList La,Lb;
    int data1[1024]={0}, data2[1024]={0};
    std::cout<<"创建La线性表。"<<std::endl;
    std::cout<<"  输入La线性表的长度num:";
    std::cin>>num;
    std::cout<<"  输入"<<num<<"个数据:";
    InitList(&La);
    for(int i=0;i<num;++i)
    {
        std::cin>>data1[i];
        ListInsert(&La, i, data1[i]);
    }
    std::cout<<"  遍历La:";
    ListTraverse(La);
    
    std::cout<<"创建Lb线性表。"<<std::endl;
    std::cout<<"  输入Lb线性表的长度num:";
    std::cin>>num;
    std::cout<<"  输入"<<num<<"个数据:";
    InitList(&Lb);
    for(int i=0;i<num;++i)
    {
        std::cin>>data2[i];
        ListInsert(&La, i, data2[i]);
    }
    std::cout<<"  遍历Lb:";
    ListTraverse(Lb);
    
    // Merge La Lb
    SqList Lc;
    std::cout<<"Merge La Lb "<<std::endl;
    std::cout<<"  线性表Lc初始化."<<std::endl;
    InitList(&Lc);
    std::cout<<"  对线性表La进行升序排列。"<<std::endl;
    IncSort(&La);
    std::cout<<"  遍历升序排列后的La:";
    ListTraverse(La);
    std::cout<<"  对线性表Lb进行升序排列。"<<std::endl;
    IncSort(&Lb);
    std::cout<<"  遍历升序排列后的Lb:";
    ListTraverse(Lb);
    std::cout<<"  归并La和Lb并创建新的Lc."<<std::endl;
    MergeList(La, Lb, &Lc);
    std::cout<<"  遍历Lc:";
    ListTraverse(Lc);
    
    // Union La Lb;
    std::cout<<"La U Lb"<<std::endl;
    UnionList(&La, Lb);
    std::cout<<"  遍历La:";
    ListTraverse(La);
}

int main(int argc, const char * argv[]) {
    test_sqlist();
    return 0;
}
